import {login,verify,loginOut,reg} from '@/services/UserService'
export default{
    namespaced:true,
    state:{
        user:null,
        isLoading:false,
        permissionList:null
    },
    mutations:{
        setIsLoading(state,payload){
            state.isLoading = payload;
        },
        setUser(state,payload){
            state.user = payload;
        },
        setPermission(state,payload){
            state.permissionList=payload;
        }
    },
    actions:{
        async fetchLogin(context,payload){
            context.commit("setIsLoading",true);
            let resp = await login(payload);
            context.commit("setUser",resp.data.data);
            console.log("*************");
            console.log(context.state.user)
            let random = context.state.user.permissionList
            // console.log(context.state.user.permissionList[0])
            context.commit("setPermission",random)
            console.log(context.state.permissionList)
            context.commit("setIsLoading",false);
        },
        async fetchVerify(context){
            context.commit("setIsLoading",true);
            let resp = await verify();
            if(resp != null){
                context.commit("setUser",resp.data.data); 
            }
            context.commit("setIsLoading",false);
        },
        async fetchReg(context,payload){
           let resp= await reg(payload);
            context.commit("setUser",resp.data.data);
        },
        loginOut(context){
            loginOut();
            context.commit("setUser",null);
        }
    }
    
}